#include<stdio.h>
#include "com_xuwei_jni_arrayoper_ArrayOper.h"

JNIEXPORT void JNICALL Java_com_xuwei_jni_arrayoper_ArrayOper_showIntArrayInfo
(JNIEnv *env, jobject obj, jintArray arr) {
	jint len = (*env)->GetArrayLength(env, arr);
	printf("length is %d\n", len);
	jint *_arr;
	_arr = (*env)->GetIntArrayElements(env, arr, NULL);
	if (_arr == NULL) {
		return NULL;
	}
	jint i;
	printf("array elements list: \n");
	for (i = 0; i < len; i++) {
		printf("%d",_arr[i]);
	}

}

JNIEXPORT jint JNICALL Java_com_xuwei_jni_arrayoper_ArrayOper_sumArray
(JNIEnv *env, jobject obj, jintArray arr) {
	jsize len = (*env)->GetArrayLength(env, arr);
	jint buf[10];
	jint i, sum = 0;
	(*env)->GetIntArrayRegion(env, arr, 0, len, buf);
	for (i = 0; i < len; i++) {
		sum += buf[i];
	}

	return sum;
}

JNIEXPORT jint JNICALL Java_com_xuwei_jni_arrayoper_ArrayOper_sumArray02
(JNIEnv *env, jobject obj, jintArray arr) {
	jint *carr;
	jint i, sum = 0;
	jint len = (*env)->GetArrayLength(env, arr);
	carr = (*env)->GetIntArrayElements(env, arr, NULL);
	if (carr == NULL) {
		return 0;
	}
	for (i = 0; i < len; i++) {
		sum += carr[i];
	}
	(*env)->ReleaseIntArrayElements(env, arr, carr, 0);
	return sum;
}

